import { _decorator, Component, instantiate, Label, math, Node, Prefab, random, randomRange, randomRangeInt, resources, tween, Vec3 } from 'cc';
import { Enemy } from './Enemy';
import { Global } from './Global';
import { Constant } from './Constant';
const { ccclass, property } = _decorator;

@ccclass('Util')
export class Util {

    static showDamageText(text: string, color: string, worldPos: Vec3, parent: Node) {
        const damageText = Global.getNode(Constant.PrefabUrl.DAMAGE_TEXT, parent);
        damageText.getComponent(Label).string = text;
        damageText.setWorldPosition(worldPos);
        damageText.getComponent(Label).color = math.color(color);

        tween(damageText)
            .to(0.1, { scale: new Vec3(1.5, 1.5, damageText.scale.z) })
            .delay(0.5)
            .to(0.1, { scale: new Vec3(0.1, 0.1, damageText.scale.z) })
            .call(() => {
                damageText.destroy();
            })
            .start();

    }

    static createEnemy(prefab: Prefab, parent: Node) {
        const enemy = instantiate(prefab);
        enemy.parent = parent;
        return enemy;
    }


    static moveNode(node: Node, radian: number, distance: number) {
        const x = node.position.x + Math.cos(radian) * distance;
        const y = node.position.y + Math.sin(radian) * distance;
        node.setPosition(x, y);

    }

    static getRadian(start: Vec3, end: Vec3) {
        return Math.atan2(end.y - start.y, end.x - start.x);
    }

}


